package unimelb.discussiontracker.twitter.web.converter;

import java.util.ArrayList;
import java.util.List;

import unimelb.discussiontracker.twitter.data.entity.TweetHashTagAssociation;
import unimelb.discussiontracker.twitter.data.entity.TweetRanking;
import unimelb.discussiontracker.twitter.data.entity.TweetUserAssociation;
import unimelb.discussiontracker.twitter.web.model.HashTagItem;
import unimelb.discussiontracker.twitter.web.model.HashTagMention;
import unimelb.discussiontracker.twitter.web.model.TweetItem;
import unimelb.discussiontracker.twitter.web.model.UserItem;
import unimelb.discussiontracker.twitter.web.model.UserMention;

public class TweetRankingToTweetItemConverter extends AbstractListConverter<TweetRanking, TweetItem> {

    @Override
    public TweetItem convertItem(TweetRanking entity) {
        final long tweetId = entity.getTweet().getId();

        final String text = entity.getTweet().getText();

        final long ownerId = entity.getTweet().getOwner().getId();

        final String owner = entity.getTweet().getOwner().getName();

        final long strength = entity.getStrength();

        final long sentiment = entity.getSentiment();

        final List<HashTagMention> tagMentions = new ArrayList<HashTagMention>();
        for (TweetHashTagAssociation assoc : entity.getTweet().getHashTags()) {
            tagMentions.add(new HashTagMention(new HashTagItem(assoc.getHashTag().getId(), assoc.getHashTag().getValue()), assoc.getStartIndex(), assoc
                    .getEndIndex()));
        }

        final List<UserMention> userMentions = new ArrayList<UserMention>();
        for (TweetUserAssociation assoc : entity.getTweet().getMentionedUsers()) {
            userMentions.add(new UserMention(new UserItem(assoc.getUser().getId(), assoc.getUser().getName()), assoc.getStartIndex(), assoc.getEndIndex()));
        }

        return new TweetItem(tweetId, text, ownerId, owner, strength, sentiment, tagMentions.toArray(new HashTagMention[tagMentions.size()]),
                userMentions.toArray(new UserMention[userMentions.size()]));
    }
}
